package com.xl.activiti.demo.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.xl.activiti.demo.service.InterfaceService;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.ActivitiException;
import org.activiti.engine.delegate.DelegateExecution;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import javax.annotation.Resource;
import java.net.URI;
import java.util.Map;

/**
 * 调用接口服务类
 * @author zhongwm
 */
@Slf4j
@Service
public class InterfaceServiceImpl implements InterfaceService {

    @Resource
    RestTemplate restTemplate;

    @Override
    public JSONObject httpGet(String url, Map<String, Object> params) {
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
        if (params != null) {
            params.forEach(builder::queryParam);
        }
        URI uri = builder.build().encode().toUri();
        ResponseEntity<JSONObject> entity = restTemplate.getForEntity(uri, JSONObject.class);
        log.info("httpGet. uri:{}, entity:{}", uri, entity);
        if (entity.getStatusCodeValue() == HttpStatus.OK.value() ) {
            return entity.getBody();
        }
        throw new ActivitiException("请求接口失败");
       // return null;
    }

    @Override
    public JSONObject httpGet(DelegateExecution execution) {
        Map<String, Object> variables = execution.getVariables();
        log.info("variables:{}, transient:{}, local:{}", variables, execution.getTransientVariables(), execution.getVariablesLocal());
        Object url = variables.get("url");

        if (url != null && StrUtil.isNotBlank(url.toString())) {
            JSONObject json = null;
            if (variables.containsKey("params")) {
                Object params = variables.get("params");
                try {
                    json = JSONUtil.parseObj(params.toString());
                } catch (Exception e) {
                    log.error("parse json error. params:{}", params.toString(), e);
                }
            }
            return httpGet(url.toString(), json);
        }
        throw new ActivitiException("请求接口为空");

        //return null;
    }

    @Override
    public JSONObject httpPost(String url, Map<String, Object> params) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        ResponseEntity<JSONObject> entity = restTemplate.postForEntity(url, new HttpEntity<>(params, headers), JSONObject.class);
        log.info("httpPost url:{}, params:{}, entity:{}", url, params, entity);
        if (entity.getStatusCodeValue() == HttpStatus.OK.value() ) {
            return entity.getBody();
        }
        throw new ActivitiException("请求接口失败");
    }

    @Override
    public JSONObject httpPost(DelegateExecution execution) {
        Map<String, Object> variables = execution.getVariables();
        Object url = variables.get("url");

        if (url != null && StrUtil.isNotBlank(url.toString())) {
            JSONObject json = null;
            if (variables.containsKey("params")) {
                Object params = variables.get("params");
                try {
                    json = JSONUtil.parseObj(params.toString());
                } catch (Exception e) {
                    log.error("parse json error. params:{}", params.toString(), e);
                }
            }
            return httpPost(url.toString(), json);
        }
        throw new ActivitiException("请求接口为空");
    }

}
